//
//  GWQAuthViewController.m
//  新浪微博
//
//  Created by qianfeng on 16/1/13.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWQAuthViewController.h"
#import "MBProgressHUD+MJ.h"
#import "GWAccount.h"
#import "GWAccountTool.h"
#import "GWControllerTool.h"
#import "GWHttpTool.h"
@interface GWQAuthViewController ()<UIWebViewDelegate>

@end

@implementation GWQAuthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIWebView *webView =[[UIWebView alloc]init];
    webView.frame =self.view.bounds;
    webView.delegate =self;
    [self.view addSubview:webView];
    NSString *url =[NSString stringWithFormat:@"https://api.weibo.com/oauth2/authorize?client_id=%@&redirect_uri=%@",client_idd,redirect_urii];
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
    [webView loadRequest:request];
    // Do any additional setup after loading the view.
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [MBProgressHUD showMessage:@"正在加载中"];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [MBProgressHUD hideHUD];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [MBProgressHUD hideHUD];
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    
    NSRange range =[request.URL.absoluteString rangeOfString:[NSString stringWithFormat:@"%@/?code=",redirect_urii]];
    if(range.length!=0)
    {
        GWAccountParam *parameter =[[GWAccountParam alloc]init];
        parameter.client_id =client_idd;
        parameter.client_secret=client_secrett;
        parameter.grant_type =@"authorization_code";
        parameter.code =[request.URL.absoluteString substringFromIndex:range.location+range.length];
        parameter.redirect_uri =redirect_urii;
        [GWAccountTool accountWithParame:parameter success:^(GWAccount *account) {
            [GWAccountTool saveAccount:account];
            [MBProgressHUD hideHUD];
            [GWControllerTool chooseRootViewController];
        } failure:^(NSError *error) {
            [MBProgressHUD hideHUD];
        }];
        /*
        NSMutableDictionary *parameter =[NSMutableDictionary dictionary];
        parameter[@"client_id"] =client_id;
        parameter[@"client_secret"]=client_secret;
        parameter[@"grant_type"] =@"authorization_code";
        parameter[@"code"] =[request.URL.absoluteString substringFromIndex:range.location+range.length];
        parameter[@"redirect_uri"] =redirect_uri;
        [GWHttpTool post:@"https://api.weibo.com/oauth2/access_token" parameters:parameter success:^(id respondObj) {
            GWAccount *account =[GWAccount accountWithDict:respondObj];
            [GWAccountTool saveAccount:account];
            [MBProgressHUD hideHUD];
            [GWControllerTool chooseRootViewController];
        } failure:^(NSError *error) {
            
            [MBProgressHUD hideHUD];
        }];
        */

        return NO;
    }
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
